package com.rytx.web.controller.statistics;

import cn.hutool.core.util.ObjectUtil;
import com.rytx.common.core.controller.BaseController;
import com.rytx.system.service.IndexService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: ysp
 * @author: zhang zb
 * @create: 2024-09-02 11:00
 **/
@RestController
@RequestMapping("/index/echarts")
public class IndexController extends BaseController {

    @Autowired
    private IndexService indexService;

    @RequestMapping("/weekEchartsData")
    public Object index() {
        Map<String, Object> result = new HashMap<>();
        List<String> weekDays = getWeekDays();
        result.put("weekDays", weekDays);
        List<Integer> userData = new ArrayList<>();
        List<Integer> orderData = new ArrayList<>();
        List<BigDecimal> incomeData = new ArrayList<>();
        List<BigDecimal> payoutData = new ArrayList<>();
        List<BigDecimal> profitData = new ArrayList<>();
        Map<String, Integer> weekArtUserCountMap = indexService.getWeekArtUserCount();
        Map<String, Integer> weekArtCount = indexService.getWeekArtCount();
        Map<String, BigDecimal> monthIncome = indexService.getWeekIncome();
        Map<String, BigDecimal> weekPayout = indexService.getWeekPayout();
        BigDecimal income = BigDecimal.ZERO;
        BigDecimal payout = BigDecimal.ZERO;
        for (String weekDay : weekDays) {
            if(ObjectUtil.isNotEmpty(weekArtUserCountMap.get(weekDay))) {
                userData.add(weekArtUserCountMap.get(weekDay));
            }
            if(!ObjectUtil.isNotEmpty(weekArtUserCountMap.get(weekDay))) {
                userData.add(0);
            }
            if(ObjectUtil.isNotEmpty(weekArtCount.get(weekDay))) {
                orderData.add(weekArtCount.get(weekDay));
            }
            if(!ObjectUtil.isNotEmpty(weekArtCount.get(weekDay))) {
                orderData.add(0);
            }
            if(ObjectUtil.isNotEmpty(monthIncome.get(weekDay))) {
                incomeData.add(monthIncome.get(weekDay));
                income = monthIncome.get(weekDay);
            }
            if(!ObjectUtil.isNotEmpty(monthIncome.get(weekDay))) {
                incomeData.add(BigDecimal.ZERO);
                income = BigDecimal.ZERO;
            }
            if(ObjectUtil.isNotEmpty(weekPayout.get(weekDay))) {
                payoutData.add(weekPayout.get(weekDay));
                payout = weekPayout.get(weekDay);
            }
            if(!ObjectUtil.isNotEmpty(weekPayout.get(weekDay))) {
                payoutData.add(BigDecimal.ZERO);
                payout = BigDecimal.ZERO;
            }
            profitData.add(income.subtract(payout));
        }
        result.put("userData", userData);
        result.put("orderData", orderData);
        result.put("totalNewUserCount",weekArtUserCountMap.values().stream().reduce(0, Integer::sum));
        result.put("totalArtWorkCount",weekArtCount.values().stream().reduce(0, Integer::sum));
        result.put("income", incomeData.stream().reduce(BigDecimal.ZERO, BigDecimal::add));
        result.put("payout", payoutData.stream().reduce(BigDecimal.ZERO, BigDecimal::add));
        result.put("profit",  incomeData.stream().reduce(BigDecimal.ZERO, BigDecimal::add).subtract(payoutData.stream().reduce(BigDecimal.ZERO, BigDecimal::add)));
        result.put("profitData", profitData);
        result.put("incomeData", incomeData);
        result.put("payoutData", payoutData);
        return result;
    }

    @RequestMapping("/monthEchartsData")
    public Object monthEchartsData() {
        Map<String, Object> result = new HashMap<>();
        List<String> monthDays = getMonthDays();
        result.put("monthDays", monthDays);
        List<Integer> userData = new ArrayList<>();
        List<Integer> orderData = new ArrayList<>();

        List<BigDecimal> incomeData = new ArrayList<>();
        List<BigDecimal> payoutData = new ArrayList<>();
        List<BigDecimal> profitData = new ArrayList<>();

        Map<String, Integer> weekArtUserCountMap = indexService.getMonthArtUserCount();
        Map<String, Integer> weekArtCount = indexService.getMonthArtCount();
        Map<String, BigDecimal> monthIncome = indexService.getMonthIncome();
        Map<String, BigDecimal> monthPayout = indexService.getMonthPayout();
        BigDecimal income = BigDecimal.ZERO;
        BigDecimal payout = BigDecimal.ZERO;
        for (String monthDay : monthDays) {
            if(ObjectUtil.isNotEmpty(weekArtUserCountMap.get(monthDay))) {
                userData.add(weekArtUserCountMap.get(monthDay));
            }
            if(!ObjectUtil.isNotEmpty(weekArtUserCountMap.get(monthDay))) {
                userData.add(0);
            }
            if(ObjectUtil.isNotEmpty(weekArtCount.get(monthDay))) {
                orderData.add(weekArtCount.get(monthDay));
            }
            if(!ObjectUtil.isNotEmpty(weekArtCount.get(monthDay))) {
                orderData.add(0);
            }
            if(ObjectUtil.isNotEmpty(monthIncome.get(monthDay))) {
                incomeData.add(monthIncome.get(monthDay));
                income = monthIncome.get(monthDay);
            }
            if(!ObjectUtil.isNotEmpty(monthIncome.get(monthDay))) {
                incomeData.add(BigDecimal.ZERO);
                income = BigDecimal.ZERO;
            }
            if(ObjectUtil.isNotEmpty(monthPayout.get(monthDay))) {
                payoutData.add(monthPayout.get(monthDay));
                payout = monthPayout.get(monthDay);
            }
            if(!ObjectUtil.isNotEmpty(monthPayout.get(monthDay))) {
                payoutData.add(BigDecimal.ZERO);
                payout = BigDecimal.ZERO;
            }
            profitData.add(income.subtract(payout));
        }
        result.put("userData", userData);
        result.put("orderData", orderData);
        result.put("totalNewUserCount",weekArtUserCountMap.values().stream().reduce(0, Integer::sum));
        result.put("totalArtWorkCount",weekArtCount.values().stream().reduce(0, Integer::sum));
        result.put("income", incomeData.stream().reduce(BigDecimal.ZERO, BigDecimal::add));
        result.put("payout", payoutData.stream().reduce(BigDecimal.ZERO, BigDecimal::add));
        result.put("profit",  incomeData.stream().reduce(BigDecimal.ZERO, BigDecimal::add).subtract(payoutData.stream().reduce(BigDecimal.ZERO, BigDecimal::add)));
        result.put("profitData", profitData);
        result.put("incomeData", incomeData);
        result.put("payoutData", payoutData);
        return result;
    }

    @RequestMapping("/yearEchartsData")
    public Object yearEchartsData() {
        Map<String, Object> result = new HashMap<>();
        List<String> monthDays = getYearMonths();
        result.put("yearMonths", monthDays);
        List<Integer> userData = new ArrayList<>();
        List<Integer> orderData = new ArrayList<>();
        Map<String, Integer> weekArtUserCountMap = indexService.getYearArtUserCount();
        Map<String, Integer> weekArtCount = indexService.getYearArtCount();

        List<BigDecimal> incomeData = new ArrayList<>();
        List<BigDecimal> payoutData = new ArrayList<>();
        List<BigDecimal> profitData = new ArrayList<>();
        Map<String, BigDecimal> yearIncome = indexService.getYearIncome();
        Map<String, BigDecimal> yearPayout = indexService.getYearPayout();
        BigDecimal income = BigDecimal.ZERO;
        BigDecimal payout = BigDecimal.ZERO;
        for (String monthDay : monthDays) {
            if(ObjectUtil.isNotEmpty(weekArtUserCountMap.get(monthDay))) {
                userData.add(weekArtUserCountMap.get(monthDay));
            }
            if(!ObjectUtil.isNotEmpty(weekArtUserCountMap.get(monthDay))) {
                userData.add(0);
            }
            if(ObjectUtil.isNotEmpty(weekArtCount.get(monthDay))) {
                orderData.add(weekArtCount.get(monthDay));
            }
            if(!ObjectUtil.isNotEmpty(weekArtCount.get(monthDay))) {
                orderData.add(0);
            }
            income = BigDecimal.ZERO;
            payout = BigDecimal.ZERO;

            if(ObjectUtil.isNotEmpty(yearIncome.get(monthDay))) {
                incomeData.add(yearIncome.get(monthDay));
                income = yearIncome.get(monthDay);
            }
            if(!ObjectUtil.isNotEmpty(yearIncome.get(monthDay))) {
                incomeData.add(BigDecimal.ZERO);
            }
            if(ObjectUtil.isNotEmpty(yearPayout.get(monthDay))) {
                payoutData.add(yearPayout.get(monthDay));
                payout = yearPayout.get(monthDay);
            }
            if(!ObjectUtil.isNotEmpty(yearPayout.get(monthDay))) {
                payoutData.add(BigDecimal.ZERO);
            }
            profitData.add(income.subtract(payout));
        }
        result.put("userData", userData);
        result.put("orderData", orderData);
        result.put("totalNewUserCount",weekArtUserCountMap.values().stream().reduce(0, Integer::sum));
        result.put("totalArtWorkCount",weekArtCount.values().stream().reduce(0, Integer::sum));
        result.put("income", incomeData.stream().reduce(BigDecimal.ZERO, BigDecimal::add));
        result.put("payout", payoutData.stream().reduce(BigDecimal.ZERO, BigDecimal::add));
        result.put("profit",  incomeData.stream().reduce(BigDecimal.ZERO, BigDecimal::add).subtract(payoutData.stream().reduce(BigDecimal.ZERO, BigDecimal::add)));
        result.put("profitData", profitData);
        result.put("incomeData", incomeData);
        result.put("payoutData", payoutData);
        return result;
    }

    // 输出当前周的日期
    public static List<String> getWeekDays() {
        List<String> weekDays = new ArrayList<>();
        LocalDate today = LocalDate.now();
        for (int i = 0; i < 7; i++) {
            LocalDate date = today.minusDays(i);
            weekDays.add(date.toString());
        }
        List<String> collect = weekDays.stream().sorted().collect(Collectors.toList());
        return collect;
    }

    public static List<String> getMonthDays() {
        List<String> weekDays = new ArrayList<>();
        LocalDate today = LocalDate.now();
        for (int i = 0; i < 30; i++) {
            LocalDate date = today.minusDays(i);
            weekDays.add(date.toString());
        }
        List<String> collect = weekDays.stream().sorted().collect(Collectors.toList());
        return collect;
    }

    public static List<String> getYearMonths() {
        LocalDate today = LocalDate.now();
        List<String> last12Months = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");

        for (int i = 0; i < 12; i++) {
            LocalDate month = today.minusMonths(i).withDayOfMonth(1);
            last12Months.add(month.format(formatter));
        }
        List<String> collect = last12Months.stream().sorted().collect(Collectors.toList());
        return collect;
    }

    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        List<String> last12Months = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");

        for (int i = 0; i < 12; i++) {
            LocalDate month = today.minusMonths(i).withDayOfMonth(1);
            last12Months.add(month.format(formatter));
        }

        // Print the results
        for (String month : last12Months) {
            System.out.println(month);
        }
    }
}
